package com.ph.website.home.controller;

import com.ph.website.home.base.common.CommonResult;
import com.ph.website.home.base.common.Ref;
import com.ph.website.home.base.component.GlobalParam;
import com.ph.website.home.base.utils.StringUtil;
import com.ph.website.home.param.HomeImgParam;
import com.ph.website.home.service.IHomeImgService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/homeImg")
public class HomeImgController {

    @Resource
    private IHomeImgService homeImgService;
    @Resource
    private GlobalParam globalParam;

    /**图片上传*/
    @RequestMapping("/upload")
    public CommonResult add(HttpServletRequest request, HttpServletResponse response){

        if(!(request instanceof MultipartHttpServletRequest)){
            System.out.println("不是文件上传");
            return Ref.error("不是文件上传");
        }
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = req.getMultiFileMap();
        if(multiFileMap==null || multiFileMap.size()==0){
            return Ref.error("上传文件为空");
        }
        MultipartFile file = multiFileMap.getFirst("file");
        if(file==null){
            return Ref.error("上传文件为空");
        }
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        int i = originalFilename.lastIndexOf(".");
        String fileName = StringUtil.randomName() + originalFilename.substring(i);
        String filePath = globalParam.getImg().getImgLocation() + "/homeImg/" + fileName;
        File imgFile = new File(filePath);
        File parentFile = imgFile.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try {
            file.transferTo(imgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String imgUrl = "/upload/homeImg/" + fileName;
        Map<String,String> map = new HashMap<>();
        map.put("id","101");
        map.put("imgUrl",imgUrl);
        map.put("prefix", globalParam.getImg().getPrefix());
        return Ref.ok(map);
    }

    /**获取所有的轮播图*/
    @RequestMapping("/listAll")
    public CommonResult listAll(){
        return homeImgService.listAll();
    }

    /**添加轮播图*/
    @RequestMapping("/add")
    public CommonResult add(@Valid HomeImgParam.AddParam param){
        return homeImgService.add(param);
    }

    /**修改轮播图*/
    @RequestMapping("/modify")
    public CommonResult modify(@Valid HomeImgParam.ModifyParam param){
        return homeImgService.modify(param);
    }

    /**删除轮播图*/
    @RequestMapping("/delete")
    public CommonResult delete(@Valid HomeImgParam.DeleteParam param){
        return homeImgService.delete(param);
    }
}
